#include <iostream>
#include <thread>
#include <mutex>

int grade = 100;
std::mutex g_grade_mutex;

/**
 * 1. 多线程中访问一个共享变量时，需要加锁，防止同时访问报错
*/
void func1()
{
    std::unique_lock<std::mutex> lock(g_grade_mutex);
    std::cout << "func1: grade = " << grade << std::endl;
}

void func2()
{
    std::unique_lock<std::mutex> lock(g_grade_mutex);
    std::cout << "func2: grade = " << grade << std::endl;
}

int main()
{
    std::thread t1(func1), t2(func2);
    t1.join();
    t2.join();
    std::cout << "end of program." << std::endl;
    return 0;
}